<?php

/**
 * Created by PhpStorm.
 * User: yuliang
 * Date: 2017/6/5
 * Time: 下午1:46
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Order extends Controller
{
    // 充值列表
    public function chargeList(){

        $search = input('search');
        $page = input('page')?input('page'):1;
        $pageSize = 10;
//        $model = model('CourseOrder');
        $model = Db::name('pay_records');
        if(trim($search)){
            $list = $model->where('user_name|order_number','like','%'.$search.'%')->page($page,$pageSize)->order('id desc')->select();
            $count = $model->where('user_name|order_number','like','%'.$search.'%')->count();
        }else{
            $list = $model->page($page,$pageSize)->order('id desc')->select();
            $count = $model->count();
        }
        $result['page'] = ceil($count/$pageSize);
        $result['data'] = $list;
        return api_return('操作成功！',$result);
    }

    // 课程购买列表
    public function saleList(){
        $search = input('search');
        $page = input('page')?input('page'):1;
        $pageSize = 10;
        $model = model('CourseOrder');
        if(trim($search)){
            $list = $model->where('course_name|user_name|teacher_name|trade_number','like','%'.$search.'%')->page($page,$pageSize)->order('id desc')->select();
            $count = $model->where('course_name|trade_number','like','%'.$search.'%')->count();
        }else{
            $list = $model->page($page,$pageSize)->order('id desc')->select();
            $count = $model->count();
        }
        $result['page'] = ceil($count/$pageSize);
        $result['data'] = $list;
        return api_return('操作成功！',$result);
    }

    // 订单发送讲义
    public function mailPaper(){
        $id = input('post.id');
        $info = Db::name('course_order')->where(['id'=>$id,'attachment'=>1,'shipments'=>0])->find();
        if($info){
            $result = Db::name('course_order')->where(['id'=>$id,'attachment'=>1,'shipments'=>0])->update(['shipments'=>1]);
            if($result){
                return api_return('发货成功！',array());
            }else{
                return api_error('50002','未知的错误');
            }
        }else{
            return api_error('50001','对不起，无效的操作！');
        }
    }

    // 用户绑定后的订单列表
    public function personCourse(){
        $uid = input('uid');
        // 绑定码之后消费订单
        $sell = Db::name('sell')->field('bind_time')->where(['uid'=>$uid])->find();
        $time = $sell['bind_time'];
        $model = model('CourseOrder');
        if($time){
            $orders = $model->field('trade_number,course_name,price,create_time')->where(['user_id'=>$uid,'status'=>1])->where('create_time','> time',$time)->select();
        }else{
            $orders = $model->field('trade_number,course_name,price,create_time')->where(['user_id'=>$uid,'status'=>1])->select();
        }
        $data['data'] = $orders;
        $data['count'] = count($orders);
        return api_return('操作成功',$data);
    }
}